<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Monolog\Logger;
use Yansongda\Pay\Pay;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
              // 往服务容器中注入一个名为 alipay 的单例对象
        $this->app->singleton(
            'alipay',
            function () {
                $config = config('pay.alipay');
                // 支付宝异步通知地址
                $config['notify_url'] = ngrok_url('payment.alipay.notify');
                // 支付成功后同步通知地址
                $config['return_url'] = route('payment.alipay.return');
                // 判断当前项目运行环境是否为线上环境
                if (app()->environment() !== 'production') {
                    $config['mode'] = 'dev';
                    $config['log']['level'] = Logger::DEBUG;
                } else {
                    $config['log']['level'] = Logger::WARNING;
                }

                // 调用 Yansongda\Pay 来创建一个支付宝支付对象
                return Pay::alipay($config);
            }
            );
            $this->app->singleton(
            'wechatpay',
            function () {
                $config = config('pay.wechat');
                // 微信支付异步通知地址
                $config['notify_url'] = ngrok_url('payment.wechat.notify');
                // 判断当前项目运行环境是否为线上环境
                if (app()->environment() !== 'production') {
                    $config['log']['level'] = Logger::DEBUG;
                } else {
                    $config['log']['level'] = Logger::WARNING;
                }

                // 调用 Yansongda\Pay 来创建一个微信支付对象
                return Pay::wechat($config);
            }
        );
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}
